#include<iostream>
#include<stdlib.h>
#include<unistd.h>

using namespace std;

bool GetQueryString(string &query_string){

    string method = getenv("METHOD");
    
    //cerr<<"METHOD:"<<(method.empty()?"method is NULL":method)<<endl;
    if(method=="GET"){
        query_string=getenv("QUERY_STRING");
        //cerr<<"query_string:"<<(query_string.empty()?"query_string is NULL":query_string)<<endl;
        return true;
    }

    else if("POST"==method){
        cerr<<"开始读取数据"<<endl; 
        int content_length=stoi(getenv("Content-Length"));
        char buffer[1024*10]={0};
        size_t n=read(0,buffer,content_length);
        
        if(n>0){
            buffer[n]='\0';
            cerr<<"quest_body:"<<buffer<<endl;
        }
        query_string=buffer;

        return true;
    }

    else{
        return false;
    }
}

void CutString(string &in,string &sep,string &out1,string &out2){

    int pos=in.find(sep);
    if(pos!=string::npos){
        out1=in.substr(0,pos);
        out2=in.substr(pos+1);
    }
}

int main(){

    string query_string;
    GetQueryString(query_string);
    string str1;
    string str2;
    string sep1="&";
    string sep2="=";
    CutString(query_string,sep1,str1,str2);

    string name1;
    string value1;
    CutString(str1,sep2,name1,value1);


    string name2;
    string value2;
    CutString(str2,sep2,name2,value2);

    cerr<<name1<<":"<<value1<<endl;
    cerr<<name2<<":"<<value2<<endl;

    cout<<name1<<":"<<value1<<endl;
    cout<<name2<<":"<<value2<<endl;

    return 0;
}